<?php


namespace app\index\model;


use think\Model;

class Todo extends Model
{
    /**
     * getTodoList
     * 获取todolist 查看同组 主播不查看
     * @url
     * @method
     * @param int $group 组id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author Asira
     * @date 2021/2/7
     */
    public static function getTodoList($group): array
    {
        $TodoClass=new self();

        $where['t.del_flag']=['=',0];
        if($group) {
            $where['t.create_group'] = ['=', $group];
        }

        $data=$TodoClass->alias('t')->join('__USER__ u1','t.create_user=u1.id','left')->join('__USER__ u2','t.complete_user=u2.id','left')->where($where)->field('t.id,t.title,t.text,t.complete_text,t.status,t.create_group `group`,t.order_id,t.customer,u1.user_name create_user_name,u2.user_name complete_user_name,DATE_FORMAT(t.create_time,\'%Y-%m-%d\') create_date')->order('t.create_time','desc')->limit(0,100)->select()->toArray();
        return $data;
    }

    public static function getTodoById($id){
        $todo=new self();
        $data=$todo->alias('t')->join('__USER__ u1','t.create_user=u1.id','left')->join('__USER__ u2','t.complete_user=u2.id','left')->where('t.id',$id)->field('t.id,t.title,t.text,t.complete_text,t.status,t.create_group `group`,t.order_id,t.customer,u1.user_name create_user_name,u2.user_name complete_user_name,DATE_FORMAT(t.create_time,\'%Y-%m-%d\') create_date')->find();
        $data['status']= $data['status'] !== 0;
        $data['is_new']= true;
        $data['edit']= false;
        return $data;
    }
}